<?php
$sUploadDir = 'public/uploads/foto-album/albums/';
$sShowDir = 'uploads/foto-album/albums/';
if (!isset ($a_sRequest [2]))
{
	$a_sAlbums = scandir ($sUploadDir);
	unset ($a_sAlbums [0],
		  $a_sAlbums [1]);
	
	echo '<p>Lijst van beschikbare foto-albums:<br /><br /></p>';
	
	if (!empty ($a_sAlbums))
	{
		foreach ($a_sAlbums as $sAlbum)
		{
			$sRandPicture = $photoAlbum -> getRandomImage ($sAlbum);

			echo '<div class="album-item">&nbsp;<a class="album-link" href="'. $a_sRequest [0] . '/' . $a_sRequest [1] . '/' . urlencode ($sAlbum) . '.html"><img class="album-cover" src="' . $sShowDir . $sAlbum . '/thumbs/' . $sRandPicture . '" /><h2 class="album-title">' . $sAlbum .'</h2></a></div><br />';
		}
	}
	else
	{
		echo 'Er zijn op dit moment geen foto-albums gevonden!';
	}
}
elseif (isset ($a_sRequest [3]))
{
	if (isset ($a_sRequest [4]) && $a_sRequest [4] == 'reageer')
	{
		if (isset ($_SESSION ['user_id']))
		{
			if ($_SERVER['REQUEST_METHOD'] == 'POST')
			{
				$sQuery = 'INSERT INTO `albumreactions` (`user_id`, `album`, `picture`, `reaction`) VALUES (:iUser_id, :sAlbum, :sPicture, :sReaction);';
				try
				{
					$oStmt = $pPDO -> prepare ($sQuery);
					$oStmt -> bindParam (':iUser_id', $_SESSION ['user_id'], PDO::PARAM_INT);
					$oStmt -> bindParam (':sAlbum', $a_sRequest [2], PDO::PARAM_STR);
					$oStmt -> bindParam (':sPicture', $a_sRequest [3], PDO::PARAM_STR);
					$oStmt -> bindParam (':sReaction', $_POST ['reaction'], PDO::PARAM_STR, 500);
					if ($oStmt -> execute ())
					{
						echo 'Je reactie is succesvol toegevoegd: <br />' . htmlentities ($_POST ['reaction']);
					}
					else
					{
						echo 'Sorry, je reactie is NIET toegevoegd door een fout. Probeer het (eventueel later) opnieuw!';
					}
				}
				catch (PDOException $e)
				{
					ePDOException :: writeMessage ($e);
				}
			}
			else
			{
				echo '<form action="" method="post"><br />';
				echo '<textarea autofocus="autofocus" maxlength="500" name="reaction" required="required" cols="40" rows="5"></textarea><br /><br />';
				echo '<button type="submit">Submit</button>';
				echo '</form>';
			}
		}
		else
		{
			echo 'Sorry, maar je moet ingelogd zijn om te kunnen reageren.';
		}
	}
	else
	{
		echo '<a href="' . substr ($_SERVER['REQUEST_URI'], 0, -5) . '/reageer.html">Plaats reactie</a>';
	}
	
	echo '<br /><br /><a id="single_image" href="' . $sShowDir . urldecode ($a_sRequest [2]) . '/' . $a_sRequest [3] .  '"><img src="' . $sShowDir . urldecode ($a_sRequest [2]) . '/thumbs/' . $a_sRequest [3] .  '" /></a>';
	
	$sQuery = 'SELECT `user`.`gebruikersnaam`, `albumreactions`.`reaction` FROM `user`, `albumreactions` WHERE `user`.`user_id`=`albumreactions`.`user_id` AND `albumreactions`.`album`=:sAlbum AND `albumreactions`.`picture`=:sPicture;';
	try
	{
		$oStmt = $pPDO -> prepare ($sQuery);
		$oStmt -> bindParam (':sAlbum', $a_sRequest [2], PDO::PARAM_STR);
		$oStmt -> bindParam (':sPicture', $a_sRequest [3], PDO::PARAM_STR);
		if ($oStmt -> execute ())
		{
			$oStmt -> bindColumn ('gebruikersnaam', $sUserName);
			$oStmt -> bindColumn ('reaction', $sReaction);
			$sReaction = htmlentities ($sReaction);
			$iReactions = $oStmt -> rowCount ();
			while ($result = $oStmt -> fetch (PDO::FETCH_BOUND))
			{
				if ($iReactions > 0)
				{
					echo '<fieldset style="border-radius: 8px; -webkit-border-radius: 8px; -moz-border-radius: 8px; width: 20%;"><legend>' . $sUserName . '</legend>' . $sReaction . '</fieldset>';
				}
				else
				{
					echo 'Sorry, maar er zijn op dit moment geen reacties beschikbaar.';
				}
			}
		}
		else
		{
			echo 'Sorry, ik kon helaas geen reacties voor je ophalen.';
		}
	}
	catch (PDOException $e)
	{
		ePDOException :: writeMessage ($e);
	}	
}
else
{
	if (file_exists ($sUploadDir . '/' . urldecode ($a_sRequest [2])))
	{
		$sRandPicture = $photoAlbum -> getRandomImage (urldecode ($a_sRequest [2]));

		if ($photoAlbum -> imageList (urldecode ($a_sRequest [2]), true) === false)
		{
			echo 'Sorry, maar er zijn op dit moment geen afbeeldingen beschikbaar.';
		}
		else
		{
			foreach ($photoAlbum -> imageList (urldecode ($a_sRequest [2]), true) as $sImage)
			{
				echo '<div><a class="album-item" rel="' . urldecode ($a_sRequest [2]) . '" href="' . $sShowDir . urldecode ($a_sRequest [2]) . '/' . $sImage .  '"><img src="' . $sShowDir . urldecode ($a_sRequest [2]) . '/thumbs/' . $sImage .  '" alt="" /></a><br /><a href="' . $a_sRequest [0] . '/' . $a_sRequest [1] . '/' . urldecode ($a_sRequest [2]) . '/' . $sImage .'.html">Reageren?</a></div><br />';
			}
		}
	}
}